package com.example.demo.repository;

import com.example.demo.entity.Borrow;
import com.example.demo.entity.PopularBook;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface BorrowMapper {

    @Select("select* from borrow where username = #{username}")
    List<Borrow> selectAllByUsername(@Param("username") String username);

    @Select("select* from borrow where bookid = #{bookid} AND username = #{username}")
    Borrow selectOne(@Param("bookid") String bookid, @Param("username")String username);

    @Insert("insert into borrow(bookid, username, expiredate, bookname) " +
            "values" +
            " ( #{bookid},#{username},#{expiredate}, #{bookname})")
    int insertOne(
            @Param("bookid") String bookid,
            @Param("username")String username,
            @Param("expiredate") Date expiredate,
            @Param("bookname")String bookname);

    @Delete("delete from borrow where bookid = #{bookid} AND username = #{username}")
    int deleteOne(@Param("bookid") String bookid,@Param("username")String username);

    @Select("select bookid, count(1) as borrowNum, bookname from borrow GROUP BY bookid, bookname ORDER BY bookid limit #{popularNum}")
    List<PopularBook> showPopularBooks(@Param("popularNum") Integer popularNum);

    @Update("update borrow " +
            "set isnearlyexpire = #{newStatus} " +
            "where username = #{username} AND bookname = #{bookname}")
    int updateExpireStatus(@Param("newStatus") int newStatus,
                           @Param("username") String username,
                           @Param("bookname")String bookname);

    @Update("update borrow set owe = #{owe} where username = #{username} AND bookname = #{bookname}")
    int updateOweFees(@Param("owe") int owe, @Param("username") String username, @Param("bookname") String bookname);
}
